<!DOCTYPE html PUBLIC "-//W4C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <title>Google Maps</title>
    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=ABQIAAAAzrOhydvk6aOjdI3StFhy1xT2yXp_ZAY8_ufC3CFXhHIE1NvwkxSGSrVHCQ1Vv7tR5qbMOUwg8Ehe0Q type="text/javascript"></script>
  </head>
  <body onunload="GUnload()">

    <!-- you can use tables or divs for the overall layout -->
    <table border=1>
      <tr>
        <td>
           <div id="map" style="width: 1200px; height: 600px"></div>
        </td>
	<td width = 200 valign="top" style="text-decoration: underline; color: #4444ff;">
           <div id="side_bar"></div>
        </td>
      </tr>
    </table>

    <noscript><b>JavaScript must be enabled in order for you to use Google Maps.</b> 
      However, it seems JavaScript is either disabled or not supported by your browser. 
      To view Google Maps, enable JavaScript by changing your browser options, and then 
      try again.
    </noscript>


    <script type="text/javascript">
    //<![CDATA[

    if (GBrowserIsCompatible()) {
      var side_bar_html = "";
    
      // arrays to hold copies of the markers used by the side_bar
      // because the function closure trick doesnt work there
      var gmarkers = [];

      var xmlDoc;

      function displayTimes(latlng) {
        var markers = xmlDoc.documentElement.getElementsByTagName("destination");
          
        for (var i = 0; i < markers.length; i++) {
          // obtain the attribues of each marker
          var lat = parseFloat(markers[i].getAttribute("lat"));
          var lng = parseFloat(markers[i].getAttribute("lng"));

	  if (lat == latlng.lat() && lng == latlng.lng()) {
	    var destTimes = markers[i].getElementsByTagName("dest-time");

	    side_bar_html = "<b>" + markers[i].getAttribute("dest-name") + "</b><br><br>";
	    side_bar_html += "Destination time(s):<br>";

            for (var j = 0; j < destTimes.length; j++) {
	      side_bar_html += '<a href="javascript:myclick(' + parseInt(markers[i].getAttribute("dest-id")) + ',' + destTimes[j].firstChild.nodeValue + ')">' + 
		      destTimes[j].firstChild.nodeValue + 'hrs <\/a><br>';
	    }
	  }

	  document.getElementById("side_bar").innerHTML = side_bar_html;
	}
      }

      // A function to create the marker and set up the event window
      function createMarker(point,name) {
        var marker = new GMarker(point);
        GEvent.addListener(marker, "click", displayTimes);
        // save the info we need to use later for the side_bar
        gmarkers.push(marker);
        // add a line to the side_bar html
        //side_bar_html += '<a href="javascript:myclick(' + (gmarkers.length-1) + ')">' + name + '<\/a><br>';
        return marker;
      }


      function myclick(dest_id,dest_time) {
        //GEvent.trigger(gmarkers[i], "click");
	alert(dest_id + dest_time);
      }


      // create the map
      var map = new GMap2(document.getElementById("map"), {draggableCursor:'url(arrow_rl.cur),default'});
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());
      map.setCenter(new GLatLng(12.966667, 77.566667), 12);

      var destAdded = false;
      var newDest;

      function clearDest() {
        if (destAdded) {
	  map.removeOverlay(newDest);
	  destAdded = false;
	}
      }

      function submitDest() {
        // Code to send dest info to server to be added
	document.location = "plot_dests.htm";
      }

      // Read the data from example.xml
      GDownloadUrl("dests.xml", function(doc) {
        xmlDoc = GXml.parse(doc);
        var markers = xmlDoc.documentElement.getElementsByTagName("destination");
          
        for (var i = 0; i < markers.length; i++) {
          // obtain the attribues of each marker
          var lat = parseFloat(markers[i].getAttribute("lat"));
          var lng = parseFloat(markers[i].getAttribute("lng"));
          var point = new GLatLng(lat,lng);
          var label = markers[i].getAttribute("dest-name");
          // create the marker
          var marker = createMarker(point,label);
          map.addOverlay(marker);
        }

        GEvent.addListener(map,"click", function(overlay,latlng) {
          if (destAdded) {
             return;
          }
 
          prompt("Enter new destination");
          newDest = new GMarker(latlng);
          map.addOverlay(newDest);
          destAdded = true;
          //alert(latlng);
        });

        // put the assembled side_bar_html contents into the side_bar div
	side_bar_html = "<a href=javascript:clearDest()>Reset</a><br>";
	side_bar_html += "<a href=javascript:submitDest()>Proceed to add route</a>";
        document.getElementById("side_bar").innerHTML = side_bar_html;
      });
    }

    else {
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }

    // This Javascript is based on code provided by the
    // Community Church Javascript Team
    // http://www.bisphamchurch.org.uk/   
    // http://econym.org.uk/gmap/

    //]]>
    </script>
  </body>

</html>

